package com.web.mall.service.impl;

import com.web.mall.mapper.SpecificationMapper;
import com.web.mall.mapper.TbSpecificationOptionMapper;
import com.web.mall.pojo.Specification;
import com.web.mall.pojo.TbSpecificationOption;
import com.web.mall.pojo.TbSpecificationOptionExample;
import com.web.mall.pojogroup.TbSpecification;
import com.web.mall.service.SpecificationManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class SpecificationManagerServiceImpl implements SpecificationManagerService {

    @Autowired
    private SpecificationMapper specificationMapper;

    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public List<Specification> querySpecification() {
        return specificationMapper.selectByExample(null);
    }


    @Override
    public void add(TbSpecification specification) {

        //保存规格，一方的数据，规格对象 没有主键
        specificationMapper.insert(specification.getSpecification()); //主键回填
        //保存规格选项  多方的数据
        for(TbSpecificationOption specificationOption:specification.getSpecificationOptions()){
            //设置规格的ID 主键回填
            specificationOption.setSpecId(specification.getSpecification().getId());

            tbSpecificationOptionMapper.insert(specificationOption);
        }
    }

    @Override
    public void update(TbSpecification specification) {
        //修改规格  一方
        specificationMapper.updateByPrimaryKey(specification.getSpecification());

        //先删除规格选项，再添加规格选项
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(specification.getSpecification().getId());

        tbSpecificationOptionMapper.deleteByExample(example);

        //修改规格选项
        for(TbSpecificationOption tbSpecificationOption:specification.getSpecificationOptions()){
            //设置规格的id
            tbSpecificationOption.setSpecId(specification.getSpecification().getId());

            tbSpecificationOptionMapper.insert(tbSpecificationOption);
        }
    }

    @Override
    public boolean deleteSpecification(Long id) {
        if(id!=null){
            //删除规格选项
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(id);
            tbSpecificationOptionMapper.deleteByExample(example);

            //删除规格
            specificationMapper.deleteByPrimaryKey(id);
            return true;
        }else {

            return false;
        }
    }

    @Override
    public boolean saveSpecification(TbSpecification specification) {
        if(ObjectUtils.isEmpty(specification.getSpecification().getId())){
            add(specification);
        }else {
            update(specification);
        }
        return true;
    }

    @Override
    public List<Specification> findSpecificationList() {
        return specificationMapper.selectByExample(null);
    }

    @Override
    public TbSpecification findTbSpecificationById(Long id) {
        TbSpecification tbSpecification = new TbSpecification();

        //根据规格id查询规格对象
        Specification specification = specificationMapper.selectByPrimaryKey(id);
        tbSpecification.setSpecification(specification);

        //根据规格的id查询规格选项
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);

        List<TbSpecificationOption> list = tbSpecificationOptionMapper.selectByExample(example);
        tbSpecification.setSpecificationOptions(list);

        return tbSpecification;
    }
}
